package news.yp.newsproject.view.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.news.yp.baselibrary.annotation.ViewById;
import com.news.yp.baselibrary.base.BaseFragment;

import java.util.List;

import news.yp.newsproject.R;
import news.yp.newsproject.bean.ResponsePageData;
import news.yp.newsproject.bean.ResponseSectionData;
import news.yp.newsproject.presenter.presenter.CenterPresenter;
import news.yp.newsproject.presenter.presenter.NewsPresenter;

/**
 * desc: 装再 新闻的fragment
 * date:18-10-28
 * who:yp666
 */
public class MainFragment extends BaseFragment<NewsPresenter> implements CenterPresenter.ViewPresenter<ResponseSectionData> {

    @ViewById(R.id.news_mainview_pager)
    private ViewPager mViewPager;
    @ViewById(R.id.news_tablayout)
    private TabLayout mTab;


    @Override
    protected void onFragmentVisibleChange(boolean b) {
        //注意此方法不会被回调，因为这个Fragment,没有使用pageFragmentAdapter(也就是Viewpager)来管理，他的setUserVisibleHint并不会被回调
    }

    @Override
    protected NewsPresenter createPresenter() {
        return new NewsPresenter();
    }

    @Override
    protected View setView(LayoutInflater inflater, ViewGroup container) {
        //You must call removeView() on the child's parent first. 最后忘记加false，就报了这个bug
        View view = inflater.inflate(R.layout.main_fragment, container, false);
        return view;
    }

    @Override
    protected void initData() {
        Log.e("TAG", "initdata");
        getPresenter().getSection();
    }

    @Override
    protected void initView() {
        initData();
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onError() {
        onSuccess(null);
    }

    @Override
    public void onSuccess(ResponseSectionData responseSectionData) {
            //创建NewsFragment
            mViewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int i) {
                    return NewsFragment.createInstance("test");
                }

                @Override
                public int getCount() {
                    return 1;
                }
            });
            mTab.setupWithViewPager(mViewPager);
            mTab.getTabAt(0).setText("test");
            mTab.getTabAt(0).select();
    }

    @Override
    public void onPageSuccess(ResponsePageData requestPageData) {

    }
}
